package com.compomics.prioritizer.general.gui;

import com.compomics.prioritizer.db.accessors.Instrument_to_user;
import com.compomics.prioritizer.db.accessors.User;
import com.compomics.prioritizer.db.accessors.Instrument;

import javax.swing.*;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.HashMap;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;

/**
 * Created by IntelliJ IDEA.
 * User: Niklaas Colaert
 * Date: 21-nov-2008
 * Time: 8:59:10
 * To change this template use File | Settings | File Templates.
 */
public class AddInstrumentGui extends JFrame {
    private JComboBox cmbUsers;
    private JTextField txtName;
    private JButton storeButton;
    private JTextArea txtDescription;
    private JPanel jContentPane;

    private User[] iUsers;
    private Connection iConn;

    public AddInstrumentGui(Connection aConn) {
        super("Create a new instrument");
        this.iConn = aConn;

        $$$setupUI$$$();
        storeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                createNewInstrument();
            }
        });

        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((screen.width / 5), (screen.height / 10));
        setContentPane(jContentPane);
        this.setSize(400, 400);
        this.setVisible(true);
    }

    public void createNewInstrument() {
        long lUserid = ((User) cmbUsers.getSelectedItem()).getUserid();
        String instrumentName = txtName.getText();
        String instrumentDescription = txtDescription.getText();

        HashMap hm = new HashMap();
        hm.put(Instrument.NAME, instrumentName);
        hm.put(Instrument.DESCRIPTION, instrumentDescription);
        Instrument inst = new Instrument(hm);

        try {
            inst.persist(iConn);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, new String[]{"Unable to store new instrument:  ", e.getMessage()}, "Error!", JOptionPane.ERROR_MESSAGE);
            return;
        }

        hm = new HashMap();
        long lInstrumentId = (Long) inst.getGeneratedKeys()[0];
        hm.put(Instrument_to_user.L_INSTRUMENTID, lInstrumentId);
        hm.put(Instrument_to_user.L_USERID, lUserid);
        Instrument_to_user lInstrument_to_user = new Instrument_to_user(hm);
        try {
            lInstrument_to_user.persist(iConn);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, new String[]{"Unable to store new instrument_to_user:  ", e.getMessage()}, "Error!", JOptionPane.ERROR_MESSAGE);
            return;
        }

        JOptionPane.showMessageDialog(this, "Stored new instrument", "Great succes!", JOptionPane.INFORMATION_MESSAGE);
        this.setVisible(false);
    }


    private void createUIComponents() {
        findUsers();

        cmbUsers = new JComboBox(iUsers);
    }


    /**
     * This method collects all users.
     * It fills the 'iUsers' cache.
     */
    private void findUsers() {
        try {
            iUsers = User.getAllOperators(iConn);
        } catch (SQLException sqle) {
            JOptionPane.showMessageDialog(this, new String[]{"Unable to find users:  ", sqle.getMessage()}, "Error!", JOptionPane.ERROR_MESSAGE);
        }
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        jContentPane = new JPanel();
        jContentPane.setLayout(new GridBagLayout());
        final JLabel label1 = new JLabel();
        label1.setText("Instrument name:");
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jContentPane.add(label1, gbc);
        final JLabel label2 = new JLabel();
        label2.setText("Instrument description:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jContentPane.add(label2, gbc);
        final JLabel label3 = new JLabel();
        label3.setText("Instrument operator:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        jContentPane.add(label3, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        jContentPane.add(cmbUsers, gbc);
        txtName = new JTextField();
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        jContentPane.add(txtName, gbc);
        txtDescription = new JTextArea();
        txtDescription.setRows(3);
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.BOTH;
        jContentPane.add(txtDescription, gbc);
        storeButton = new JButton();
        storeButton.setText("Store");
        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        jContentPane.add(storeButton, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return jContentPane;
    }
}
